import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.*;

import java.util.Iterator;
import java.util.Vector;
import java.io.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;

public class AgentAnLexicala extends Agent
{
protected void setup()
{
	addBehaviour(new CyclicBehaviour(this){

AnalizaLexicala al=new AnalizaLexicala();
String[] cuvlinie = null,clinie;;
//Iterator<String> it = linii.iterator();
String linie;
boolean gasit=false;
int index=0;
String file=BlackBoard._TXT_PATH+"log.txt";
	
public void action()
{
ACLMessage msg = receive();
if (msg!=null) {

 linie=msg.getContent();
 //System.out.println(linie);
 clinie = linie.split("\\|");
 linie=clinie[0];
 index=Integer.parseInt(clinie[1]);
    cuvlinie = linie.split(" ");	
    
    //Verific analiza lexicala
	try{
	     gasit=	al.verifica(cuvlinie, index);
	   }catch(Exception e) 
	   {
		   WriteFile.write("Exceptie1 la linia "+index+"\n"+e.getMessage(),file);
		   }
	
	   //Daca trece de analiza lexicala trimite linia analizei sintactice
	   if(gasit==true) sendMessage(linie+"|"+index);
	   else WriteFile.write("Eroare lexicala la linia "+index,file);
index++;
//if(index==linii.size())
//{System.out.println("Nu mai sunt linii de analizat"); 
//doDelete();}
//}//end while
}
block(100);
	}
	});
}

//trimiterea unui mesaj
	private void sendMessage(String linie){
		AID r=new AID ("AgentAnSintactica@"+getHap(),AID.ISGUID);
		ACLMessage msg=new ACLMessage(ACLMessage.REQUEST);
		msg.addReceiver(r);		
		msg.setContent(linie);
		this.send(msg);
			
	}
	
protected void takeDown() {
//	 Printout a dismissal message
	System.out.println(this.getLocalName()+" terminating");
	}
}
